package com.geeklan.smart.ui.dialog;

import android.content.Context;
import android.view.LayoutInflater;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.geeklan.smart.R;
import com.geeklan.smart.databinding.DialogContentDateBinding;
import com.geeklan.smart.utils.DateUtils;
import com.geeklan.smart.utils.DensityUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class DateBottomSheetDialog extends BaseBottomSheetDialog<DialogContentDateBinding>{

    private List<String> yearList;
    private List<String> monthList;
    private List<String> dayList;
    private String birthDateString;

    public void setCurrentDate(String birthDateString) {
        this.birthDateString = birthDateString;
        setupWheelViews();
    }

    private OnDateSelectedListener onDateSelectedListener;

    public DateBottomSheetDialog(Context context) {
        super(context);
    }

    @Override
    protected DialogContentDateBinding createContentViewBinding(LayoutInflater inflater) {
        return DialogContentDateBinding.inflate(inflater);
    }

    @Override
    protected void setupDialog() {
        super.setupDialog();
        birthDateString = DateUtils.dateFormatDay();
        yearList = new ArrayList<>();
        monthList = new ArrayList<>();
        dayList = new ArrayList<>();
        setConfirmButtonVisible(true);
        setDialogTitle(mContext.getString(R.string.pet_edit));
        setNonFullScreen(0.5f);
        mBinding.wvYear.setUnit(mContext.getString(R.string.year));
        mBinding.wvMonth.setUnit(mContext.getString(R.string.month));
        mBinding.wvDay.setUnit(mContext.getString(R.string.day));
        initData();
        setupWheelViews();
        setConfirmButtonClickListener(v -> {
            if (onDateSelectedListener != null) {
                String selectedYear = yearList.get(mBinding.wvYear.getCurrentItem());
                String selectedMonth = monthList.get(mBinding.wvMonth.getCurrentItem());
                String selectedDay = dayList.get(mBinding.wvDay.getCurrentItem());
                if(selectedMonth.length() == 1){
                    selectedMonth = "0" + selectedMonth;
                }
                if(selectedDay.length() == 1){
                    selectedDay = "0" + selectedDay;
                }
                onDateSelectedListener.onDateSelected(selectedYear, selectedMonth, selectedDay);
            }
            dismiss();
        });

    }

    private void initData() {
        // 获取当前日期
        Calendar currentCalendar = Calendar.getInstance();
        int currentYear = currentCalendar.get(Calendar.YEAR);
        int currentMonth = currentCalendar.get(Calendar.MONTH) + 1;
        int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);

        // 初始化年份
        for (int year = 1970; year <= currentYear; year++) {
            yearList.add(String.valueOf(year));
        }
        // 初始化月份
        for (int month = 1; month <= 12; month++) {
            monthList.add(String.valueOf(month));
        }

        // 初始化日期（默认展示1月数据）
        for (int day = 1; day <= 31; day++) {
            dayList.add(String.valueOf(day));
        }
    }

    private void setupWheelViews() {
        // 设置年份数据
        mBinding.wvYear.setCyclic(false); // 禁止循环滚动
        mBinding.wvYear.setAdapter(new ArrayWheelAdapter<>(yearList));
        mBinding.wvYear.setOnItemSelectedListener(index -> updateMonthAndDay());
        mBinding.wvYear.setCurrentItem(DateUtils.getYear(birthDateString) - 1970);
        // 设置月份数据
        mBinding.wvMonth.setCyclic(true);
        mBinding.wvMonth.setAdapter(new ArrayWheelAdapter<>(monthList));
        mBinding.wvMonth.setOnItemSelectedListener(index -> updateDay());
        mBinding.wvMonth.setCurrentItem(DateUtils.getMonth(birthDateString) - 1);
        // 设置日期数据
        mBinding.wvDay.setCyclic(true);
        mBinding.wvDay.setAdapter(new ArrayWheelAdapter<>(dayList));
        mBinding.wvDay.setCurrentItem(DateUtils.getDay(birthDateString) - 1);

        updateMonthAndDay();
    }

    private void updateMonthAndDay() {
        int selectedYear = Integer.parseInt(yearList.get(mBinding.wvYear.getCurrentItem()));

        // 更新月份
        Calendar currentCalendar = Calendar.getInstance();
        int currentYear = currentCalendar.get(Calendar.YEAR);
        int currentMonth = currentCalendar.get(Calendar.MONTH) + 1;

        monthList.clear();
        for (int month = 1; month <= (selectedYear == currentYear ? currentMonth : 12); month++) {
            monthList.add(String.valueOf(month));
        }
        mBinding.wvMonth.setAdapter(new ArrayWheelAdapter<>(monthList));
        // 修正当前月份索引
        int currentMonthItem = mBinding.wvMonth.getCurrentItem();
        if (currentMonthItem >= monthList.size()) {
            currentMonthItem = monthList.size() - 1;
        }
        mBinding.wvMonth.setCurrentItem(currentMonthItem);

        updateDay();
    }

    private void updateDay() {
        int selectedYear = Integer.parseInt(yearList.get(mBinding.wvYear.getCurrentItem()));
        int selectedMonth = Integer.parseInt(monthList.get(mBinding.wvMonth.getCurrentItem()));

        // 获取当前日期
        Calendar currentCalendar = Calendar.getInstance();
        int currentYear = currentCalendar.get(Calendar.YEAR);
        int currentMonth = currentCalendar.get(Calendar.MONTH) + 1;
        int currentDay = currentCalendar.get(Calendar.DAY_OF_MONTH);

        // 更新日期
        dayList.clear();
        int maxDay = getDaysInMonth(selectedYear, selectedMonth);
        for (int day = 1; day <= ((selectedYear == currentYear && selectedMonth == currentMonth) ? currentDay : maxDay); day++) {
            dayList.add(String.valueOf(day));
        }
        mBinding.wvDay.setAdapter(new ArrayWheelAdapter<>(dayList));
        // 修正当前日期索引
        int currentDayItem = mBinding.wvDay.getCurrentItem();
        if (currentDayItem >= dayList.size()) {
            currentDayItem = dayList.size() - 1;
        }
        mBinding.wvDay.setCurrentItem(currentDayItem);
        mBinding.wvDay.setCurrentItem(0);
    }

    private int getDaysInMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public void setOnDateSelectedListener(OnDateSelectedListener listener) {
        this.onDateSelectedListener = listener;
    }

    public interface OnDateSelectedListener {
        void onDateSelected(String year, String month, String day);
    }
}
